package com.fzg.common.demo.readWriteTxt;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * create by fzg
 * 2022/11/17 14:53
 * 写入TXT文件
 */

@Slf4j
public class WriteTxtFile {

    public static void main(String[] args) {
        write();
    }

    public static void write() {
        Scanner scanner = new Scanner(System.in);
        // 输入：用Scanner类的hasNextLine方法判断
        System.out.println("请输入：");
        String str = "";
        if (scanner.hasNextLine()){
            str = scanner.nextLine();
            log.info(str);
        }
        if (str.equals("")){
            System.out.println("输入为空");
        }else {
            File file = new File("E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\readWriteTxt\\demo01.txt");
            FileWriter fw;
            try {
                fw = new FileWriter(file,true);
                try {
                    fw.write(str);
                    fw.close();
                    System.out.println("文件写入成功！");
                }catch (Exception e){
                    System.out.println("文件写入失败");
                }
                fw.close();
            } catch (IOException e) {
                System.out.println("未找到文件");
                e.printStackTrace();
            }
        }
    }
}
